package com.synnex.mobile.servier.web.common.security.mobile;

import com.synnex.mobile.generic.core.json.JSONModel;
import com.synnex.mobile.generic.web.session.WebSession;
import com.synnex.mobile.servier.web.common.security.CommonGroup;
import com.synnex.mobile.servier.web.common.session.mobile.MobileWebSession;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 *
 * @author Lance Chen
 */
public class MobileGroup extends CommonGroup {

    private static final long serialVersionUID = 1L;

    @Override
    public boolean request(HttpServletRequest request, HttpServletResponse response) {
        boolean pass = super.request(request, response);
        if (pass) {

            return pass;
        } else {

            try {
                response.setContentType(JSONModel.CONTENT_TYPE);
                JSONModel.create().setStatus(JSONModel.STATUS.NEEDLOGIN).write(response.getWriter());
                return false;
            } catch (IOException ex) {
                logger.error(ex.getMessage(), ex);
                return false;
            }
        }
    }

    @Override
    protected MobileWebSession getSession(HttpServletRequest request) {
        MobileWebSession webSession = WebSession.getCurrentSession(MobileWebSession.class, request);
        return webSession;
    }

    @Override
    protected MobileWebSession createSession(HttpServletRequest request) {
        return WebSession.create(MobileWebSession.class, request);
    }
}
